package com.example.patentback.controller;

import cn.hutool.core.map.MapUtil;
import com.example.patentback.entity.SysRole;
import com.example.patentback.entity.SysRoleMenu;
import com.example.patentback.service.SysRoleMenuService;
import com.example.patentback.service.SysRoleService;
import com.example.patentback.service.SysUserRoleService;
import com.example.patentback.service.SysUserService;
import com.example.patentback.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/role")
public class SysRoleController {

    @Autowired
    SysRoleService sysRoleService;

    @Autowired
    SysRoleMenuService sysRoleMenuService;

    @Autowired
    SysUserService sysUserService;

    @Autowired
    SysUserRoleService sysUserRoleService;

    //获取角色信息和相关联的所有菜单id
    @GetMapping("/roleInfo/{id}")
    @PreAuthorize("hasAuthority('sys:role:list')")
    public Result roleInfo(@PathVariable(name = "id") Long id) {
        SysRole sysRole = sysRoleService.selectById(id);
        List<SysRoleMenu> sysRoleMenus = sysRoleMenuService.selectRoleMenuByRoleId(id);
        List<Long> menuIds = sysRoleMenus.stream().map(p -> p.getMenuId()).collect(Collectors.toList());
        sysRole.setMenuIds(menuIds);
        return Result.success(sysRole);
    }

    //查询所有角色进行分页
    @GetMapping("/select")
    @PreAuthorize("hasAuthority('sys:role:list')")
    public Result selectByName(String name, int current, int size) {
        List<SysRole> sysRoles = sysRoleService.selectByName(name, current, size);
        return Result.success(MapUtil.builder()
                .put("data", sysRoles)
                .put("current", 1)
                .put("size", size)
                .build());
    }

    @PostMapping("/save")
    @PreAuthorize("hasAuthority('sys:role:save')")
    public Result saveRole(@Validated @RequestBody SysRole sysRole) {
        sysRoleService.saveSysRole(sysRole);
        return Result.success(sysRole);
    }

    @PostMapping("/update")
    @PreAuthorize("hasAuthority('sys:role:update')")
    public Result updateRole(@Validated @RequestBody SysRole sysRole) {
        sysRoleService.updateSysRole(sysRole);
        //更新权限缓存
        sysUserService.clearUserAuthorityInfoByRoleId(sysRole.getId());
        return Result.success(sysRole);
    }

    //批量删除角色
    @PostMapping("/delete")
    @Transactional
    @PreAuthorize("hasAuthority('sys:role:delete')")
    //@PreAuthorize("hasAnyAuthority('sys:role:delete')")
    public Result deleteRole(@RequestBody Long[] ids) {
        sysRoleService.deleteSysRoles(ids);
        //更新权限缓存
        Arrays.stream(ids).forEach(id -> {
            sysUserService.clearUserAuthorityInfoByRoleId(id);
        });
        //删除角色用户关联表和角色菜单关联表相关信息
        sysRoleMenuService.deleteRoleMenuByRoleId(ids);
        sysUserRoleService.deleteSysUserRoleByRoleId(ids);
        return Result.success("删除成功");
    }

    //更新角色de菜单权限
    @PostMapping("/change/{roleId}")
    @PreAuthorize("hasAuthority('sys:role:perm')")
    public Result changeRoleMenu(@PathVariable(name = "roleId") Long roleId, @RequestBody Long[] menuIds) {
        sysRoleMenuService.updateRoleMenu(roleId, menuIds);
        return Result.success("分配成功");
    }
}
